package abg.core.domain.sys;

import com.alibaba.fastjson2.JSON;

import java.util.HashMap;

/**
 * SRS系统统一返回数据对象
 * 1.用于返回对象
 * 2.用于创建实体
 */
public class SRS<T> extends HashMap {
    //     * 请求状态（true成功/false失败）
    private boolean success;
    //     * 状态对应编码（200成功；500失败；其他.....）
    private int code;
    //     * 返回消息（操作成功/操作失败/其他）
    private String message;
    //     * 响应数据
    private T data;
    //     * 响应时间戳
    private String timestamp;

    public static <T> SRS<T> create() {
        return new SRS<T>();
    }

    public static <T> SRS<T> bySuccess() {
        return by(MSG.SU_200.code(), MSG.SU_200.message(), null, true);
    }

    public static <T> SRS<T> bySuccess(Object data) {
        return by(MSG.SU_200.code(), MSG.SU_200.message(), data, true);
    }

    public static <T> SRS<T> bySuccess(int code, String msg, Object data) {
        return by(code, msg, data, true);
    }

    public static <T> SRS<T> byError() {
        return by(MSG.ER_400.code(), MSG.ER_400.message(), null, false);
    }

    public static <T> SRS<T> byError(MSG msg) {
        return by(msg.code(), msg.message(), null, false);
    }

    public static <T> SRS<T> byError(String msg) {
        return by(MSG.ER_400.code(), msg, null, false);
    }

    public static <T> SRS<T> byError(MSG msg, Object data) {
        return by(msg.code(), msg.message(), data, false);
    }

    public static <T> SRS<T> byError(int code, String msg, Object data) {
        return by(code, msg, data, true);
    }

    private static <T> SRS<T> by(int code, String msg, Object data, boolean success) {
        SRS<Object> timestamp1 = create()
                .code(code)
                .message(msg)
                .data(data)
                .success(success)
                .timestamp(System.currentTimeMillis());
        return (SRS<T>) timestamp1;
    }

    public SRS<T> data(T data) {
        super.put("data", data);
        return this;
    }

    public SRS<T> success(boolean success) {
        super.put("success", success);
        return this;
    }

    public SRS<T> timestamp(long timestamp) {
        super.put("timestamp", timestamp);
        return this;
    }

    public SRS<T> message(Object message) {
        super.put("message", message);
        return this;
    }

    public SRS<T> code(int code) {
        super.put("code", code);
        return this;
    }

    public SRS<T> set(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public Object get(Object key) {
        return super.get(key);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public String toJSONString() {
        return JSON.toJSONString(this);
    }

    public boolean isSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public String getTimestamp() {
        return timestamp;
    }
}
